<?php
	define('CONFIG_FILE',true);
    include '../config.php';
    
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]=="Therapy Assistant"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }

	$sqlDBManager = new SqlDBManager();

	$id = intval($_REQUEST['AttendanceRecordID']);
	$username = $_SESSION['username'];
	
	$sql = "Select * from attendancerecord where AttendanceRecordID=? AND Status=?";
	$parameters=array($id,'Not Verified');
	$res=$sqlDBManager->queryRows($sql,$parameters);

	if (!empty($res)) {
	    $sql = "UPDATE attendancerecord SET Status=?, AuditVerify=? where AttendanceRecordID=?";
	    $parameters=array('Verified',$username,$id);
		$res=$sqlDBManager->execute_dml($sql,$parameters);

		//close connection
  		$sqlDBManager->close_connect();

	    if ($res>0){
	        echo json_encode(array('success' => true));
	    }else{
	        echo json_encode(array('msg' => 'Update attendance record failed due to database error.'));
	    }
	}else{
		//close connection
  		$sqlDBManager->close_connect();

	    echo json_encode(array('msg' => 'Attendance record has already been verified.'));
	}
?>